package com.wtb.domain;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.wtb.qq.domain.QqOauth;
import com.wtb.rr.domain.RrOauth;
@SuppressWarnings("unchecked")
public class DataCache {
	public static HashMap<String, RrOauth> rrmap = new HashMap<String, RrOauth>();
	public static HashMap<String, QqOauth> qqmap = new HashMap<String, QqOauth>();
	public static ConcurrentHashMap<String, String> rrlastTime = new ConcurrentHashMap<String, String>();
	public static ConcurrentHashMap<String, String> qqlastTime = new ConcurrentHashMap<String, String>();
	public static AtomicInteger value = new AtomicInteger();
	public static AtomicInteger count = new AtomicInteger();
	public static boolean RR_MAP_FLAG = false;
	public static boolean QQ_MAP_FLAG = false;
	public static boolean RR_TIME_FLAG = false;
	public static boolean QQ_TIME_FLAG = false;
	public static ArrayBlockingQueue<String> mailQeque = new ArrayBlockingQueue<String>(10);
	public static int getValue(){
		return value.get();
	}
	public static int increment(){
		return value.incrementAndGet();
	}
	public static int decrement(){
		return value.decrementAndGet();
	}
	public static int increment(int i){
		return value.addAndGet(i);
	}
	
	
	public static int getCount(){
		return count.get();
	}
	public static int countIncrement(){
		return count.incrementAndGet();
	}
	public static int countIncrement(int i){
		return count.addAndGet(i);
	}
	

	public static HashMap<String,RrOauth> synRrMap(){
		if (DataCache.RR_MAP_FLAG) {
			synchronized (DataCache.rrmap) {
				DataCache.RR_MAP_FLAG = false ; 
			}
		}
		HashMap<String, RrOauth> hashMap = (HashMap<String, RrOauth>) DataCache.rrmap.clone();
		for (Entry<String, RrOauth> entry : hashMap.entrySet()) {
			try {
				hashMap.put(entry.getKey(), (RrOauth) entry.getValue().clone());
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
		}
		return hashMap;
	}
	
	public static HashMap<String,QqOauth> synQqMap(){
		if (DataCache.QQ_MAP_FLAG) {
			synchronized (DataCache.qqmap) {
				DataCache.QQ_MAP_FLAG = false ; 
			}
		}
		HashMap<String, QqOauth> hashMap = (HashMap<String, QqOauth>) DataCache.qqmap.clone();
		for (Entry<String, QqOauth> entry : hashMap.entrySet()) {
			try {
				hashMap.put(entry.getKey(), (QqOauth) entry.getValue().clone());
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
		}
		return hashMap;
		
		
	}
	
	public static void synPutRrMap(RrOauth rrOauth){
		if (DataCache.RR_MAP_FLAG) {
			synchronized (DataCache.rrmap) {
				DataCache.RR_MAP_FLAG = false ; 
			}
		}
		DataCache.rrmap.put(rrOauth.getUni(), rrOauth);
	}
	
	public static void synPutQqMap(QqOauth qqOauth){
		if (DataCache.QQ_MAP_FLAG) {
			synchronized (DataCache.qqmap) {
				DataCache.QQ_MAP_FLAG = false ; 
			}
		}
		DataCache.qqmap.put(qqOauth.getUni(), qqOauth);
	}
	
	public static void synRemRrMap(String key){
		if (DataCache.RR_MAP_FLAG) {
			synchronized (DataCache.rrmap) {
				DataCache.RR_MAP_FLAG = false ; 
			}
		}
		DataCache.rrmap.remove(key);
	}
	
	public static void synRemQqMap(String key){
		if (DataCache.QQ_MAP_FLAG) {
			synchronized (DataCache.qqmap) {
				DataCache.QQ_MAP_FLAG = false ; 
			}
		}
		DataCache.qqmap.remove(key);
	}
	
	public static String synRrTime(String key) {
		if (DataCache.RR_TIME_FLAG) {
			synchronized (DataCache.rrlastTime) {
				DataCache.RR_TIME_FLAG = false;
			}
		}
		return DataCache.rrlastTime.get(key);
	}
	
	public static String synQqTime(String key) {
		if (DataCache.QQ_TIME_FLAG) {
			synchronized (DataCache.qqlastTime) {
				DataCache.QQ_TIME_FLAG = false;
			}
		}
		return DataCache.qqlastTime.get(key);
	}
	
 }
